package com.example.module_motocircle.wigth

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.lib_common.base.utils.ToastUtils
import com.example.module_motocircle.R
import com.example.module_motocircle.adpater.NinePicsAdapter
import com.example.module_motocircle.model.Image
import com.example.module_motocircle.model.detailImage


/**
 *@author Wcj
 *@description
 *@date 2022/7/5 14:50
 */
class NineGridPicsView : FrameLayout {
    private val mView: View
    private val mRvPic : RecyclerView

    private var mPicsList:List<Image>? = null // 图片集合
    private var mDetailPicsList:List<detailImage>? = null // 图片集合
    private val mItemDecoration by lazy{ //分割线
        SpacesItemDecoration(10)
    }


    constructor(context: Context):this(context,null)
    constructor(context: Context, attrs: AttributeSet?):this(context,attrs,0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int):super(context,attrs,defStyleAttr){
        mView = LayoutInflater.from(context).inflate(R.layout.layout_nine_grid_pics_view,this)
        mRvPic = mView.findViewById(R.id.rv_pic_9)
    }

    /**
     * 设置图片资源
     */
    fun setPicsList(list:List<Image>){
        this.mPicsList = list
    }


    /**
     * 初始化view,将图片展示在recyclerview中
     */
    fun initView(){
        if(mPicsList.isNullOrEmpty()){
            return
        }
        val hasSize = ! mPicsList!!.any { it.imageHeight == 0 || it.imageWidth == 0 } // 判断图片是否有尺寸
        val columns = if(hasSize && mPicsList!!.size == 1) 1 else  3 //列数3
        val lg = object :GridLayoutManager(context,columns,RecyclerView.VERTICAL,false) {
            override fun canScrollHorizontally(): Boolean { //不能横向滑动
                return false
            }

            override fun canScrollVertically(): Boolean {//不能纵向滑动
                return false
            }
        }
        mRvPic.layoutManager = lg
        //适配器
        val nineAdapter = NinePicsAdapter(mPicsList!!){
            ToastUtils.showLong(context,"点击了第${it+1}张图片")
        }
        mRvPic.adapter = nineAdapter
//        //分割线
        mRvPic.removeItemDecoration(mItemDecoration)
        mRvPic.addItemDecoration(mItemDecoration)

    }
}